package com.demo.oauth.exceptions;

import com.demo.commons.results.CommonEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import lombok.SneakyThrows;

/**
* @author yangjian
* @version 创建时间：2022年2月14日 下午2:38:55
* 
*/
public class MyAuth2ExceptionSerializer extends StdSerializer<MyAuth2Exception> {

	public MyAuth2ExceptionSerializer() {
		super(MyAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(MyAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
		gen.writeStartObject();
		if(CommonEnum.ACCESS_TOKEN_ERROR.getCode().equals(value.getErrorCode())) {
			gen.writeObjectField("code", CommonEnum.ACCESS_TOKEN_ERROR.getCode());
			gen.writeStringField("msg", CommonEnum.ACCESS_TOKEN_ERROR.getMsg());
		}else if(CommonEnum.REFRESH_TOKEN_ERROR.getCode().equals(value.getErrorCode())){
			gen.writeObjectField("code", CommonEnum.REFRESH_TOKEN_ERROR.getCode());
			gen.writeStringField("msg", CommonEnum.REFRESH_TOKEN_ERROR.getMsg());
		}else if(CommonEnum.AUTH_VER.getCode().equals(value.getErrorCode())) {
			gen.writeObjectField("code", CommonEnum.AUTH_VER.getCode());
			gen.writeStringField("msg", CommonEnum.AUTH_VER.getMsg());
		}
		gen.writeEndObject();
	}

}
